//
//  YJTrainingListTableViewController.swift
//  yuejian
//
//  Created by yanziye on 2017/10/11.
//  Copyright © 2017年 gyy. All rights reserved.
//

import UIKit

class YJTrainingListTableViewController: UITableViewController {
    
    @IBOutlet var tableview: UITableView!
    var trainingList: NSArray = NSArray()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        getTrainingList()
        }
    
    
    
    func getTrainingList(){
        
        YJNetworkMngTool.sharedNetWorkMngTool().YJNetwork_GetTrainingListView(self.view){
            (trainingList) -> Void in
            print(trainingList)
            self.trainingList = trainingList
            self.tableview.reloadData()
        }
        
    }
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var basecell:UITableViewCell = UITableViewCell()
        
        let cell = YJTrainingTableViewCell.creatCell(tableview)
        let currenttraining:YJTrainingModel = self.trainingList[(indexPath as NSIndexPath).row] as! YJTrainingModel
        cell.training_name.text = (currenttraining.training_name as NSString) as String
        let imageStr = "http://172.24.10.175/workout/Uploads/" + (currenttraining.thumb! as String)
        let imageUrl = URL(string: imageStr)
        let data = try! Data(contentsOf: imageUrl!)
        let image = UIImage(data: data)
        cell.thumbb.image = image
        
        basecell = cell
        return basecell
        
        
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        if self.trainingList.count == 0{
            return 0
        }
        return self.trainingList.count
    }
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt: IndexPath) -> CGFloat
    {
        
        return 150
        
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let currenttraining:YJTrainingModel = trainingList[(indexPath as NSIndexPath).row] as! YJTrainingModel
        // let foodDic: NSDictionary = ["food": currentFood,"phonenum":self.shop.phonenum]
        self.performSegue(withIdentifier: "toTraining", sender: currenttraining)
        
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        let trainingDetailController = segue.destination as! TrainingDetailTableViewController
        trainingDetailController.train = sender as! YJTrainingModel
        
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    

}
    


